package rexsee.core.widget;

import rexsee.core.menu.PopMenuItem;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;

public class TabButton extends Tab {

	public TabButton(final Context context, final PopMenuItem item) {
		super(context, item);
		this.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					styleSheet = stylePressed;
					setLabelStyle();
					if (downRunnable != null) downRunnable.run();
				} else if (action == MotionEvent.ACTION_UP) {
					if (styleSheet.equals(stylePressed)) {
						cancelPressed();
						if (extraRunnable != null) extraRunnable.run();
						if (upRunnable != null) upRunnable.run();
					}
				} else if (action == MotionEvent.ACTION_CANCEL) {
					cancelPressed();
				} else if (action == MotionEvent.ACTION_MOVE) {
					float x = event.getX();
					float y = event.getY();
					if (x < 0 || x > v.getWidth() || y < 0 || y > v.getHeight()) cancelPressed();
				}
				return false;
			}
			private void cancelPressed() {
				styleSheet = styleNormal;
				setLabelStyle();
			}
		});
	}

}
